package review;

import lev01_beginnerClass.util.HelpUtil;

import javax.crypto.spec.PSource;
import java.util.Arrays;

public class Review02 {
    public static void main(String[] args) {
        int[] arr = HelpUtil.generateRandomArr(20, 20);
        System.out.println(Arrays.toString(arr));
        inserSortV2(arr);
        System.out.println(Arrays.toString(arr));
        int[] count = new int[2];

        for(int i = 0; i< 1000;i++){
            count[f01fair()]++;
        }
        System.out.println(Arrays.toString(count));



    }

    public static void selectionSort(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        int len = arr.length;

        for(int i = 0;i < len;i++){
            int minIndex = i;
            for(int j = i+1;j < len;j++){
                minIndex = arr[j] < arr[minIndex]? j:minIndex;
            }
            swap(arr,i,minIndex);
        }
    }

    public static void bubleSort(int[] arr){
        if(arr == null || arr.length <2){
            return;
        }
        int len = arr.length;
        for(int i = 0;i < len-1;i++){
            for(int j = 1;j < len;j++){
                if(arr[j] < arr[j-1]){
                    swap(arr,j,j-1);
                }
            }
        }
    }

    public static void inserSort(int[] arr){
        if(arr == null || arr.length <2){
            return;
        }
        int len = arr.length;
        for(int i = 1;i < len;i++){
            for(int j = i;j >= 1 && arr[j-1] > arr[j];j--){
                swap(arr,j,j-1);
            }
        }
    }

    public static void inserSortV2(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        int len = arr.length;

        for(int i = 1; i<len;i++ ){
            int newIndex = i;
            while(newIndex >=1 && arr[newIndex] > arr[newIndex -1]){
                swap(arr,newIndex,newIndex-1);
                newIndex-- ;
            }
//

        }
    }

    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static int f01unfair(){
        return Math.random() < 0.8? 0:1;
    }

    public static int f01fair(){
        int ans = 0;
        do {
            ans = f01unfair();
        }while (ans == f01unfair());
        return ans;
    }

}
